﻿Imports System.Data
Imports System.Data.SqlClient

Public Class cita
#Region "atributos"
    Dim _tratamiento As String
    Dim _paciente As Paciente
    Dim _fecha As Date
    Dim con As Conexion
    Dim id_cita As Int32
#End Region
#Region "propiedades"
    Public ReadOnly Property Tratamiento() As String
        Get
            Return Me._tratamiento
        End Get
    End Property
    Public ReadOnly Property Fecha() As Date
        Get
            Return Me._fecha
        End Get
    End Property
    Public ReadOnly Property Paciente() As Paciente
        Get
            Return Me._paciente
        End Get
    End Property
    Public ReadOnly Property id() As Int32
        Get
            Return id_cita
        End Get
    End Property
#End Region
#Region "metodos"
    Public Sub New(ByVal tratamiento As String, ByVal fecha As Date, ByVal paciente As Paciente)
        Me._tratamiento = tratamiento
        Me._fecha = fecha
        Me._paciente = paciente

        con = New Conexion
        con.Conectar()

        Try
            Dim comando As New SqlCommand("crear_cita", con.SqlCon)
            comando.CommandType = CommandType.StoredProcedure
            comando.Parameters.AddWithValue("@tratamiento", Me._tratamiento)
            comando.Parameters.AddWithValue("@fecha", Me._fecha)
            comando.Parameters.AddWithValue("@paciente", Me._paciente.Id_cliente)
            comando.ExecuteNonQuery()

        Catch ex As Exception
            MsgBox("Error al crear el paciente" & vbNewLine & ex.Message, MsgBoxStyle.Critical, "Error!")
        End Try
        con.Desconectar()
        con = Nothing
    End Sub

    Public Sub New(ByVal id As Int32)
        Me.id_cita = id
        Dim fila As DataRow
        con = New Conexion
        con.Conectar()

        Try
            fila = con.getDatarow("select tratamiento, id_paciente, fecha from cita where id_cita =" & id)
            Me._tratamiento = fila(0).ToString
            Me._paciente = New Paciente(Int32.Parse(fila(1).ToString))
            Me._fecha = Date.Parse(fila(2).ToString)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
        End Try

        con.Desconectar()
        con = Nothing
    End Sub

    Public Sub Eliminar()
        con = New Conexion
        con.Conectar()

        Try
            Dim comando As New SqlCommand("eliminar_cita", con.SqlCon)
            comando.CommandType = CommandType.StoredProcedure
            comando.Parameters.AddWithValue("@id_cita", Me.id_cita)
            comando.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error!")
        End Try
        con.Desconectar()
        con = Nothing

    End Sub

    Public Sub modificar(ByVal tratamiento As String, ByVal fecha As Date)
        con = New Conexion
        con.Conectar()

        Dim comando As New SqlCommand("modificar_cita", con.SqlCon)
        comando.CommandType = CommandType.StoredProcedure

        comando.Parameters.AddWithValue("@tratamiento", tratamiento)
        comando.Parameters.AddWithValue("@fecha", fecha)

        comando.ExecuteNonQuery()
        con.Desconectar()
    End Sub
    Protected Overrides Sub Finalize()
        MyBase.Finalize()
        con = Nothing
        Me._fecha = Nothing
        Me.id_cita = Nothing
        Me._paciente = Nothing
        Me._tratamiento = Nothing
    End Sub
#End Region
End Class
